package test.my.com.myapplicationtest.comment_textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import test.my.com.myapplicationtest.R;

/**
 * Created by ThinkPad on 2016/2/22.
 * http://aichixihongshi.iteye.com/blog/1207503 关于SpannableString讲解
 */
public class CommentTextView<T> extends TextView {

    public static final String DEFAULT_NAME_TEXT_COLOR = "#1081e0";
    public static final String DEFAULT_COMMENT_CONTENT_TEXT_COLOR = "#666666";
    public static final float DEFAULT_COMMENT_TEXT_SIZE = 28f;

    private Context mContext;
    private T t1;
    private T t2;

    public CommentTextView(Context context) {
        super(context);
        initData(context);
    }

    public CommentTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initData(context);
    }

    public CommentTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData(context);
    }

    private void initData(Context context){
        this.mContext = context;
        this.setTextSize(DEFAULT_COMMENT_TEXT_SIZE);
        this.setTextColor(Color.parseColor(DEFAULT_COMMENT_CONTENT_TEXT_COLOR));
    }

    /**
     * 添加评论
     * @param commentName
     * @param commentContent
     * @param t
     * @param onNameClickListener
     */
    public void addComment(String commentName,String commentContent,final T t,final OnNameClickListener onNameClickListener){
        SpannableString nameSpannableStr = new SpannableString(commentName);
        nameSpannableStr.setSpan(new MyClickableSpan() {
            @Override
            public void onClick(View widget) {
                super.onClick(widget);
                if(onNameClickListener != null){
                    onNameClickListener.clickCommentName(t);
                }
            }
        }, 0, commentName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        this.append(nameSpannableStr);
        this.setMovementMethod(LinkMovementMethod.getInstance());

        String content = "："+commentContent;
        this.append(content);

    }

    public void addReply(String replyName,String commentName,String replyContent,final T t,final OnNameClickListener onNameClickListener){

        String tempStr = " 回复 ";

        SpannableString replyNameSpannableStr = new SpannableString(replyName);
        replyNameSpannableStr.setSpan(new MyClickableSpan() {
            @Override
            public void onClick(View widget) {
                super.onClick(widget);
                if (onNameClickListener != null) {
                    onNameClickListener.clickReplyName(t);
                }
            }
        }, 0, replyName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        this.append(replyNameSpannableStr);
        this.setMovementMethod(LinkMovementMethod.getInstance());
        this.append(tempStr);

        SpannableString commentNameSpannableStr = new SpannableString(commentName);
        commentNameSpannableStr.setSpan(new MyClickableSpan() {
            @Override
            public void onClick(View widget) {
                super.onClick(widget);
                if(onNameClickListener != null){
                    onNameClickListener.clickCommentName(t);
                }
            }
        }, 0, commentName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        this.append(commentNameSpannableStr);
        this.setMovementMethod(LinkMovementMethod.getInstance());

        tempStr = "："+replyContent;
        this.append(tempStr);
    }


    class MyClickableSpan extends ClickableSpan {

        @Override
        public void onClick(View widget) {

        }

        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(Color.parseColor(DEFAULT_NAME_TEXT_COLOR));
        }
    }

    interface OnNameClickListener<T>{
        void clickCommentName(T t);
        void clickReplyName(T t);
    }


}
